/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetlif13.model.puissance4;

import projetlif13.model.Game2D;
import projetlif13.model.Move;
import projetlif13.model.Player;

/**
 *
 * @author theo
 */
public class P4Game extends Game2D {

    public P4Game(Player _p1, Player _p2) {
        super(_p1, _p2);
        this.gameBoard = new P4Board();
        if (_p1 instanceof IAPlayerP4) {
            ((IAPlayerP4) _p1).start();
        }
        if (_p2 instanceof IAPlayerP4) {
            ((IAPlayerP4) _p2).start();
        }
    }

    @Override
    public boolean gameEnded() {
        for (int y = 0; y < gameBoard.getHeight(); y++) {
            for (int x = 0; x < gameBoard.getWidth(); x++) {
                if (gameBoard.getPIDByXY(x, y) != -1) {
                    int x2 = x;
                    int y2 = y;
                    int tempNb = 0;
                    if (y > 2) {//Diagonale droite montante && monté
                        while (gameBoard.getPIDByXY(x2, y2) == gameBoard.getPIDByXY(x, y)) {
                            x2++;
                            y2--;
                            tempNb++;
                            if (x2 >= gameBoard.getWidth() || y2 <= 0) {
                                break;
                            }
                        }
                        if (tempNb > 3) {
                            return true;
                        }
                        x2 = x;
                        y2 = y;
                        tempNb = 0;
                        while (gameBoard.getPIDByXY(x2, y2) == gameBoard.getPIDByXY(x, y)) {
                            y2--;
                            tempNb++;
                            if (x2 >= gameBoard.getWidth() || y2 <= 0) {
                                break;
                            }
                        }
                        if (tempNb > 3) {
                            return true;
                        }
                        x2 = x;
                        y2 = y;
                        tempNb = 0;
                    }
                    if (y < 3) {//diagonale droite  déscendante && déscente
                        while (gameBoard.getPIDByXY(x2, y2) == gameBoard.getPIDByXY(x, y)) {
                            x2++;
                            y2++;
                            tempNb++;
                            if (x2 >= gameBoard.getWidth() || y2 >= gameBoard.getHeight()) {
                                break;
                            }
                        }
                        if (tempNb > 3) {
                            return true;
                        }
                        x2 = x;
                        y2 = y;
                        tempNb = 0;

                        while (gameBoard.getPIDByXY(x2, y2) == gameBoard.getPIDByXY(x, y)) {
                            y2++;
                            tempNb++;
                            if (x2 >= gameBoard.getWidth() || y2 >= gameBoard.getHeight()) {
                                break;
                            }
                        }
                        if (tempNb > 3) {
                            return true;
                        }
                        x2 = x;
                        y2 = y;
                        tempNb = 0;
                    }
                    while (gameBoard.getPIDByXY(x2, y2) == gameBoard.getPIDByXY(x, y)) {
                        x2++;
                        tempNb++;
                        if (x2 >= gameBoard.getWidth() || y2 >= gameBoard.getHeight()) {
                            break;
                        }
                    }
                    if (tempNb > 3) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean moveAvailable(Move _m) {
        for (int y = 0; y < gameBoard.getHeight(); y++) {
            for (int x = 0; x < gameBoard.getWidth(); x++) {
                if (gameBoard.getPIDByXY(x, y) == -1) {
                    return true;
                }
            }
        }
        return false;
    }
}
